package com.busionline.wotu.baselibrary.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

/**
 * 项目名称：joke
 * 类描述：可以添加头部和底部的recycleview
 * 创建人：woochen123
 * 创建时间：2017/10/1 11:36
 * 必须要先调用setAdapter方法后才能添加和移除头部（否则）
 */
public class WrapRecyclerView extends RecyclerView {
    private WrapRecyclerAdapter mWrapAdapter;
    public WrapRecyclerView(Context context) {
        super(context);
    }

    public WrapRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public WrapRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setAdapter(Adapter adapter) {
        //替换系统的adapter
        mWrapAdapter = new WrapRecyclerAdapter(adapter);
        super.setAdapter(mWrapAdapter);
    }

    /**
     * 添加头部View
     * @param view
     */
    public void addHeadView(View view){
        if (mWrapAdapter == null) {
           throw new IllegalArgumentException("please invoke setAdapter() first!");
        }
        mWrapAdapter.addHeaderView(view);
    }

    /**
     * 移除头部View
     * @param view
     */
    public void removeHeaderView(View view){
        if(mWrapAdapter != null){
            mWrapAdapter.removeHeaderView(view);
        }
    }

    /**
     * 添加尾部View
     * @param view
     */
    public void addFooterView(View view){
        if (mWrapAdapter == null) {
            throw new IllegalArgumentException("please invoke setAdapter() first!");
        }
        mWrapAdapter.addFooterView(view);
    }

    /**
     * 移除底部View
     * @param view
     */
    public void removeFooterView(View view){
        if(mWrapAdapter != null){
            mWrapAdapter.removeFooterView(view);
        }
    }

}
